// Estruturas para desenho das ranhuras
#ifndef RANHURAS
#define RANHURAS

#include <GL/Glut.h>
#include <math.h> 

struct pontos
{
	float x,y;
};

class ranhuras
{
public:
	pontos *pex;
	pontos *pin;
	float raioex, raiomd, raioin;
	float dente;	//dente=ranhura
	int tam;
	char init(float ex, float in);
	void drawrancir(void);

//  void drawrancir_ps(int inst, alimentacao *al, enrolamento *enrol, FILE *arq);
//  void drawranrect(int inst, alimentacao *al, enrolamento *enrol);
//  void drawranrect_ps(int inst, alimentacao *al, enrolamento *enrol, FILE *arq);
	~ranhuras(void);
};

char ranhuras::init(float ex, float in)
{
	int i;
	float ang,angr,pi;
	float rstep,esp,test=0.4;
	
	pi=(float)(4*atan(1));
	
	esp=(ex-in);

	raioin=in;
	raiomd=in+esp/2;
	raioex=ex;
	esp=esp/2;
	
	dente=(float)360/(2*24);
	rstep=2*test*dente/6;
	
	for(ang=-dente*test,tam=0;ang<=dente*test;ang+=rstep,tam++);
	pex=new pontos[2*tam];
	pin=new pontos[2*tam];

	if(pex==0 || pin==0) { printf(MSG123);exit(1);}

	esp*=0.9;
	for(i=0,ang=-dente*test;ang<=dente*test;ang+=rstep,i++)
		{
			angr=ang*pi/180;	
			pin[i].x= -(in+esp)*cos(angr);
			pin[i].y= -(in+esp)*sin(angr);
		}
	for(ang=dente*test;ang>=-dente*test;ang-=rstep,i++)
		{
			angr=ang*pi/180;
			pin[i].x= -(in)*cos(angr);
			pin[i].y= -(in)*sin(angr);
		}
	
	in+=(esp/0.9)*1.1;

	for(i=0,ang=-dente*test;ang<=dente*test;ang+=rstep,i++)
		{
			angr=ang*pi/180;
			pex[i].x= -(ex)*cos(angr);
			pex[i].y= -(ex)*sin(angr);
		}
	for(ang=dente*test;ang>=-dente*test;ang-=rstep,i++)
		{
			angr=ang*pi/180;
			pex[i].x= -(in)*cos(angr);
			pex[i].y= -(in)*sin(angr);
		}
	tam=i;
	return 0;
}

ranhuras::~ranhuras(void)
{
	delete [] pin;
	delete [] pex;
	printf(MSG124);
}

	
void ranhuras::drawrancir(void)
{
	int i,j;
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

	for(i=0;i<24;i++)
	{
		glBegin(GL_QUAD_STRIP);
		for(j=0;j<tam/2;j++)
		{
			glVertex2f(pin[j].x,pin[j].y);
			glVertex2f(pin[tam-1-j].x,pin[tam-1-j].y);
		}
		glEnd();

		glBegin(GL_QUAD_STRIP);
		for(j=0;j<tam/2;j++)
		{
			glVertex2f(pex[j].x,pex[j].y);
			glVertex2f(pex[tam-1-j].x,pex[tam-1-j].y);
		}
		glEnd();

		glRotatef(2*dente,0,0,-1);
	}
	glPopMatrix();
}

/*
void ranhuras::drawrancir_ps(int inst, alimentacao *al, enrolamento *enrol, FILE *arq)
{
	int i,		// contador
		rant,   // valor temporario do status (inicio ou fim) da bobina na ranhura
		phase;	// valor temporario para n. de fases
	float f[4],		// vetor com as intensidades das correntes por fase
		  cor[4],	// vetor com o valor da cor da corrente em cada fase
		  pang,		// metade do abertura angular de uma ranhura em graus
		  pangr,	// idem acima, mas em radianos
		  pi;		// constante pi
	
	pi=(float)(4*atan(1));
	pang=dente/2;
	pangr=pang*pi/180;	
	phase=enrol->f;

	// logica para carregar correntes e setar cores
	// fez-se valor da cor = (valor da corrente)^2
	if (phase==3)
	{
		f[1]=al->am3f[inst].r;
		f[2]=al->am3f[inst].s;
		f[3]=al->am3f[inst].t;
		cor[1]=f[1]*f[1];
		cor[2]=f[2]*f[2];
		cor[3]=f[3]*f[3];		
	}
	if (phase==2)
	{
		f[1]=al->am2f[inst].d;
		f[2]=al->am2f[inst].q;
		cor[1]=f[1]*f[1];
		cor[2]=f[2]*f[2];
	}

	cor[1]=1-cor[1];
	cor[2]=1-cor[2];
	cor[3]=1-cor[3];
	
	// Salva estado do PostScript
	fprintf(arq,"gsave\n");

	fprintf(arq,"0.4 setlinewidth\n");
	
	// Comeca a desenhar as ranhuras...
	for(i=0;i<enrol->numranhuras;i++)
	{
		// verifica se o condutor entra ou sai da ranhura interna e usa a cor apropriada
		rant=enrol->ran[i].in.val;
		if (rant*f[abs(rant)]<0) 
			fprintf(arq,"%f %f %f setrgbcolor\n", cor[abs(rant)], 1.0, 1.0);
		else 
			fprintf(arq,"%f %f %f setrgbcolor\n", 1.0, 1.0, cor[abs(rant)]);

		// desenha a ranhura
		fprintf(arq, "newpath\n");
		fprintf(arq, "\t%f pt %f pt moveto\n", -raiomd*cos(-pangr), -raiomd*sin(-pangr));
		fprintf(arq, "\t0 0 %f pt %f %f arc\n", raiomd, -pang+180, pang+180);
		fprintf(arq, "\t%f pt %f pt lineto\n", -raioin*cos(pangr), -raioin*sin(pangr));
		fprintf(arq, "\t0 0 %f pt %f %f arcn\n", raioin, pang+180, -pang+180);
		fprintf(arq, "closepath\n");
		fprintf(arq, "fill\n");	
		fprintf(arq, "stroke\n");
		
		// verifica se o condutor entra ou sai da ranhura externa e escolhe a cor como acima
		rant=enrol->ran[i].ex.val;
		if (rant*f[abs(rant)]<0) 
			fprintf(arq,"%f %f %f setrgbcolor\n", cor[abs(rant)], 1.0, 1.0);
		else 
			fprintf(arq,"%f %f %f setrgbcolor\n", 1.0, 1.0, cor[abs(rant)]);
		
		// desenha a ranhura
		fprintf(arq, "newpath\n");
		fprintf(arq, "\t%f pt %f pt moveto\n", -raioex*cos(-pangr), -raioex*sin(-pangr));
		fprintf(arq, "\t0 0 %f pt %f %f arc\n", raioex, -pang+180, pang+180);
		fprintf(arq, "\t%f pt %f pt lineto\n", -raiomd*cos(pangr), -raiomd*sin(pangr));
		fprintf(arq, "\t0 0 %f pt %f %f arcn\n", raiomd, pang+180, -pang+180);
		fprintf(arq, "closepath\n");
		fprintf(arq, "fill\n");		
		fprintf(arq, "stroke\n");
		
		// desenha pequena borda ao redor da ranhura
		fprintf(arq,"gsave\n");
		fprintf(arq, "%s setrgbcolor\n",COR_LIN_RAN_PS);
		fprintf(arq, "0.4 pt setlinewidth\n");
		fprintf(arq, "1 setlinecap\n");
		fprintf(arq, "1 setlinejoin\n");
		fprintf(arq, "newpath\n");
		fprintf(arq, "\t%f pt %f pt moveto\n", -raiomd*cos(-pangr), -raiomd*sin(-pangr));
		fprintf(arq, "\t0 0 %f pt %f %f arc\n", raiomd, -pang+180, pang+180);
		fprintf(arq, "\t%f pt %f pt lineto\n", -raioin*cos(pangr), -raioin*sin(pangr));
		fprintf(arq, "\t0 0 %f pt %f %f arcn\n", raioin, pang+180, -pang+180);
		fprintf(arq, "closepath\n");
		fprintf(arq, "stroke\n");
		
		fprintf(arq, "newpath\n");
		fprintf(arq, "\t%f pt %f pt moveto\n", -raioex*cos(-pangr), -raioex*sin(-pangr));
		fprintf(arq, "\t0 0 %f pt %f %f arc\n", raioex, -pang+180, pang+180);
		fprintf(arq, "\t%f pt %f pt lineto\n", -raiomd*cos(pangr), -raiomd*sin(pangr));
		fprintf(arq, "\t0 0 %f pt %f %f arcn\n", raiomd, pang+180, -pang+180);
		fprintf(arq, "closepath\n");
		fprintf(arq, "stroke\n");
		fprintf(arq,"grestore\n");
				
		// roda o sistema de coordenada para desenhar as proximas duas ranhuras
		fprintf(arq, "%f rotate\n", -2*dente);
	}
	// restaura estado PostScript
	fprintf(arq,"grestore\n");
}


void ranhuras::drawranrect(int inst, alimentacao *al, enrolamento *enrol)
{
	int i, j, rant, phase;
	int nran;
	char c[3];
	float passo, tamx1, tamx2, tamy, tamyr1, tamyr2, place, off;
	float f[4],cor[4];

	phase=enrol->f;
	if (phase==3)
	{
		f[1]=al->am3f[inst].r;
		f[2]=al->am3f[inst].s;
		f[3]=al->am3f[inst].t;
		cor[1]=f[1]*f[1];
		cor[2]=f[2]*f[2];
		cor[3]=f[3]*f[3];		
	}

	if (phase==2)
	{
		f[1]=al->am2f[inst].d;
		f[2]=al->am2f[inst].q;
		cor[1]=f[1]*f[1];
		cor[2]=f[2]*f[2];
	}

	nran=2*enrol->f*enrol->q;
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

	passo=(float)(TAM_J3_X)/(float)(2*nran);

	tamy=TAM_J3_Y-TAM_COROA;
	tamyr1=tamy+TAM_COROA*ESPES_RANHURA;
	tamyr2=tamy+TAM_COROA*2*ESPES_RANHURA;
	off=(TAM_J3_Y-tamyr2)/2; place=off*0.1;
	off=off*0.9;
		
	for(i=0;i<=nran;i++)
	{
		if(i==nran) j=0; else j=i;
		
		rant=enrol->ran[j].in.val;
		
		c[0]=64+fabs(rant);
		c[1]=(rant<0) ? '\'' : 0 ;
		c[2]=0;
		
		glLineWidth(1);
		glColor3f(COR_TEXTO_J3);
		strokeOutput(0.1,2*passo*i, tamyr2+place, c);

		if (rant*f[abs(rant)]<0) 
			glColor3f(cor[abs(rant)],0,0);
		else 
			glColor3f(0,0,cor[abs(rant)]);

		tamx1=2*passo*i-0.5*passo;
		tamx2=2*passo*i+0.5*passo;

		glBegin(GL_QUADS);
			glVertex2f(tamx1,tamyr1);
			glVertex2f(tamx1,tamy);
			glVertex2f(tamx2,tamy);
			glVertex2f(tamx2,tamyr1);
		glEnd();

		rant=enrol->ran[j].ex.val;
		
		c[0]=64+fabs(rant);
		c[1]=(rant<0) ? '\'' : 0;
		c[2]=0;
		
		glColor3f(COR_TEXTO_J3);
		glLineWidth(1);
		strokeOutput(0.1,2*passo*i, tamyr2+off+place, c);
		
		if (rant*f[abs(rant)]<0) 
			glColor3f(cor[abs(rant)],0,0);
		else 
			glColor3f(0,0,cor[abs(rant)]);
		
		glBegin(GL_QUADS);
			glVertex2f(tamx1,tamyr2);
			glVertex2f(tamx1,tamyr1);
			glVertex2f(tamx2,tamyr1);
			glVertex2f(tamx2,tamyr2);
		glEnd();
	}
	glPopMatrix();
}


void ranhuras::drawranrect_ps(int inst, alimentacao *al, enrolamento *enrol, FILE *arq)
{
	int i, j, rant, phase;
	int nran;
	char c[3];
	float passo, tamx1, tamx2, tamy, tamyr1, tamyr2, place, off;
	float f[4],cor[4];

	phase=enrol->f;
	if (phase==3)
	{
		f[1]=al->am3f[inst].r;
		f[2]=al->am3f[inst].s;
		f[3]=al->am3f[inst].t;
		cor[1]=f[1]*f[1];
		cor[2]=f[2]*f[2];
		cor[3]=f[3]*f[3];		
	}

	if (phase==2)
	{
		f[1]=al->am2f[inst].d;
		f[2]=al->am2f[inst].q;
		cor[1]=f[1]*f[1];
		cor[2]=f[2]*f[2];
	}

	nran=2*enrol->f*enrol->q;
	passo=(float)(TAM_J3_X)/(float)(2*nran);
	tamy=TAM_J3_Y-TAM_COROA;
	tamyr1=tamy+TAM_COROA*ESPES_RANHURA;
	tamyr2=tamy+TAM_COROA*2*ESPES_RANHURA;
	off=(TAM_J3_Y-tamyr2)/2; place=off*0.1;
	off=off*0.9;
	
	// preparo fontes
	fprintf(arq,"gsave\n");
	fprintf(arq,"/showtext {moveto show} bind def\n");
	fprintf(arq,"/showcentertext\n");
	fprintf(arq,"{\n");
	fprintf(arq,"\tgsave\n");
	fprintf(arq,"\t/yi exch def\n");
	fprintf(arq,"\t/xi exch def\n");
	fprintf(arq,"\t/texto exch def\n");
	fprintf(arq,"\t0 setlinewidth\n");
	fprintf(arq,"\tnewpath\n");
	fprintf(arq,"\t0 0 moveto\n");
	fprintf(arq,"\ttexto false charpath\n");
	fprintf(arq,"\tflattenpath\n");
	fprintf(arq,"\tpathbbox\n");
	fprintf(arq,"\t/y2 exch def\n");
	fprintf(arq,"\t/x2 exch def\n");
	fprintf(arq,"\t/y1 exch def\n");
	fprintf(arq,"\t/x1 exch def\n");
	fprintf(arq,"\ty2 y1 sub 2 div -1 mul\n");
	fprintf(arq,"\t/offy exch def\n");
	fprintf(arq,"\tx2 x1 sub 2 div -1 mul\n");
	fprintf(arq,"\t/offx exch def\n");
	fprintf(arq,"\ttexto xi offx add yi offy add showtext\n");
	fprintf(arq,"\tgrestore\n");
	fprintf(arq,"} bind def\n\n");
	fprintf(arq,"/%s findfont\n",TXT_FONT_NAME_PS);
	fprintf(arq,"%f scalefont setfont\n",(float)(TXT_FONT_SIZE_PS));
	
	for(i=0;i<=nran;i++)
	{
		if(i==nran) j=0; else j=i;
		
		rant=enrol->ran[j].in.val;
		
		c[0]=64+fabs(rant);
		c[1]=(rant<0) ? '\'' : 0 ;
		c[2]=0;
		
		fprintf(arq,"%s setrgbcolor\n", COR_TXT_PS);
		fprintf(arq,"\t(%s) %f pt %f pt showcentertext\n", c, 2*passo*i, tamyr2+place+off*0.3);	
				
		fprintf(arq,"%f pt setlinewidth\n", (float)(TAM_LIN_BORDA_PS));
		if (rant*f[abs(rant)]<0) 
			fprintf(arq,"%f %f %f setrgbcolor\n", cor[abs(rant)], 1.0, 1.0);
		else 
			fprintf(arq,"%f %f %f setrgbcolor\n", 1.0, 1.0, cor[abs(rant)]);
		
		if(i==0) tamx1=2*passo*i; else tamx1=2*passo*i-0.5*passo;
		if(i==nran) tamx2=2*passo*i; else tamx2=2*passo*i+0.5*passo;

		fprintf(arq,"%f pt setlinewidth\n", (float)TAM_LIN_BORDA_PS);
		fprintf(arq,"newpath\n");
		fprintf(arq,"\t%f pt %f pt moveto\n",tamx1,tamyr1);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx1,tamy);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx2,tamy);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx2,tamyr1);
		fprintf(arq,"closepath\n");
		fprintf(arq,"fill\n");
		fprintf(arq,"stroke\n");

		fprintf(arq,"%s setrgbcolor\n", COR_BORDA_PS);
		fprintf(arq,"newpath\n");
		fprintf(arq,"\t%f pt %f pt moveto\n",tamx1,tamyr1);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx1,tamy);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx2,tamy);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx2,tamyr1);
		fprintf(arq,"closepath\n");
		fprintf(arq,"stroke\n");
				
		rant=enrol->ran[j].ex.val;
		
		c[0]=64+fabs(rant);
		c[1]=(rant<0) ? '\'' : 0;
		c[2]=0;
		
		fprintf(arq,"%s setrgbcolor\n", COR_TXT_PS);
		fprintf(arq,"\t(%s) %f pt %f pt showcentertext\n", c, 2*passo*i, tamyr2+off+place);	
				
		fprintf(arq,"%f pt setlinewidth\n", (float)(TAM_LIN_BORDA_PS));
		if (rant*f[abs(rant)]<0) 
			fprintf(arq,"%f %f %f setrgbcolor\n", cor[abs(rant)], 1.0, 1.0);
		else 
			fprintf(arq,"%f %f %f setrgbcolor\n", 1.0, 1.0, cor[abs(rant)]);
		
		fprintf(arq,"%f pt setlinewidth\n", (float)TAM_LIN_BORDA_PS);
		fprintf(arq,"newpath\n");
		fprintf(arq,"\t%f pt %f pt moveto\n",tamx1,tamyr2);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx1,tamyr1);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx2,tamyr1);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx2,tamyr2);
		fprintf(arq,"closepath\n");
		fprintf(arq,"fill\n");
		fprintf(arq,"stroke\n");

		fprintf(arq,"%s setrgbcolor\n", COR_BORDA_PS);
		fprintf(arq,"newpath\n");
		fprintf(arq,"\t%f pt %f pt moveto\n",tamx1,tamyr2);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx1,tamyr1);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx2,tamyr1);
		fprintf(arq,"\t%f pt %f pt lineto\n",tamx2,tamyr2);
		fprintf(arq,"closepath\n");
		fprintf(arq,"stroke\n");
	}
	fprintf(arq,"grestore\n");
}*/

#endif